<?php
namespace app\admin\controller;

use think\Request;
use app\admin\model\User;

class Posted extends Common
{
    //待审核列表
    public function audit()
    {
        $where = [];
        $where['is_del'] = 0;
        $where['status'] = 2;
        $userModel = new User();
        $data = $userModel->getAllLists($where);
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }
    //发布者列表
    public function posteder(Request $request)
    {
        $where = [];
        $where['is_del'] = 0;
        if ($request->param('mobile')) {
            $where['mobile'] = $request->param('mobile', '', 'trim');
            $this->assign('mobile', $where['mobile']);
        }
        if ($request->param('status')) {
            $where['status'] = $request->param('status', 1, 'intval');
            $this->assign('status', $where['status']);
        }
        if ($request->param('starttime') && $request->param('endtime')) {
            $starttime = $request->param('starttime', '', 'trim');
            $endtime = $request->param('endtime', '', 'trim');
            $where['create_time'] = ['between', [
                strtotime($starttime),
                strtotime($endtime) + 86400,
            ]];
            $this->assign('starttime', $starttime);
            $this->assign('endtime', $endtime);
        } elseif ($request->param('starttime')) {
            $starttime = $request->param('starttime', '', 'trim');
            $where['create_time'] = [
                'egt',
                strtotime($starttime),
            ];
            $this->assign('starttime', $starttime);
        } elseif ($request->param('endtime')) {
            $endtime = $request->param('endtime', '', 'trim');
            $where['create_time'] = [
                'elt',
                strtotime($endtime) + 86400,
            ];
            $this->assign('endtime', $endtime);
        }
        if ($request->param('user_name')) {
            $user_name = $request->param('user_name', '', 'trim');
            $where['user_name'] = ['like', '%'.$user_name.'%'];
            $this->assign('user_name', $user_name);
        }
        $userModel = new User();
        $data = $userModel->getAllLists($where);
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }
    //删除账号
    public function postederDelete(Request $request)
    {
        if ($request->isPost()) {
            $id = $request->post('id', 0, 'intval');
            if (!$id) {
                $this->error('参数错误');
            }
            $userModel = new User();
            if ($userModel->userDel($id)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
    //禁用账号
    public function postederUpstatus(Request $request)
    {
        if ($request->isPost()) {
            $id = $request->post('id', 0, 'intval');
            $status = $request->post('status', 1, 'intval');
            if (!$id || !in_array($status, [1, 4])) {
                $this->error('参数错误');
            }
            $userModel = new User();
            if ($userModel->userUpStatus($id, $status, ['status'=>['neq', 2]])) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
    //审核账户
    public function postederAudit(Request $request)
    {
        if ($request->isPost()) {
            $id = $request->post('id', 0, 'intval');
            $status = $request->post('status', 1, 'intval');
            $deny = $request->post('deny', '', 'filterString');
            if (!$id || !in_array($status, [1, 3])) {
                $this->error('参数错误');
            } elseif ($status == 3) {
                if (!$deny || mb_strlen($deny) > 30) {
                    $this->error('拒绝原因不正确');
                }
            }
            $userModel = new User();
            if ($userModel->userUpStatus($id, $status, ['status'=>2], [
                'audit_time' => time(),
                'audit_info' => $deny
            ])) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
}
